package com.healthai.controller;

import com.healthai.dto.response.ApiResponse;
import com.healthai.entity.User;
import com.healthai.service.HealthService;
import com.healthai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Map;

@RestController
@RequestMapping("/api/health")
public class HealthController {
    
    @Autowired
    private HealthService healthService;
    @Autowired
    private UserService userService;
    /**
     * 获取当前用户ID
     * @return 用户ID
     */
    private Integer getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        // 这里需要从用户名获取用户ID，可以通过UserService实现
        User user = userService.getUserByUsername(username);
        return user.getUserId();
    }
    
    /**
     * 获取健康建议
     * @return 健康建议
     */
    @GetMapping("/advice")
    public ApiResponse<String> getHealthAdvice() {
        Integer userId = getCurrentUserId();
        String advice = healthService.getHealthAdvice(userId);
        return ApiResponse.success(advice);
    }
    
    /**
     * 获取健康报告
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 健康报告
     */
    @GetMapping("/report")
    public ApiResponse<Map<String, Object>> getHealthReport(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        Integer userId = getCurrentUserId();
        Map<String, Object> report = healthService.getHealthReport(userId, startDate, endDate);
        return ApiResponse.success(report);
    }
}